package com.aoxin.service.impl.course.systemCourse;

import com.aoxin.mapper.course.systemCourse.AloneCourseToPageDao;
import com.aoxin.model.course.AloneCourseToPage;
import com.aoxin.service.inter.course.systemCourse.AloneCourseToPageService;
import com.aoxin.util.ErrorUtil;
import com.aoxin.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.LinkedList;
import java.util.Map;

@Service("AloneCourseToPageService")
public class AloneCourseToPageServiceImpl implements AloneCourseToPageService {
    @Autowired
    private AloneCourseToPageDao AloneCourseToPageDao;

    /**
     * 给某个包插入课程
     *
     * @param courseIds
     * @param coursePageId
     * @return
     */
    @Override
    public Integer insertCourseToPage(Integer courseIds[], Integer coursePageId) throws Exception {
        Integer checkNumber = 0;
        String schoolId = String.valueOf(ThreadLocalUtil.getEmployee().getSchoolId());
        Assert.notNull(schoolId, ErrorUtil.CHECK_SCHOOL_ID);
        Assert.notEmpty(courseIds, ErrorUtil.CHECK_ADD_TO_PAGE);
        Assert.notNull(coursePageId, ErrorUtil.CHECK_ADD_TO_PAGE);
        int i = 0;
        for (Integer courseId : courseIds) {
            Integer check = AloneCourseToPageDao.checkCourseToPage(courseId, coursePageId, schoolId);
            if (check == 0) {
                checkNumber++;
            } else {
                throw new Exception(ErrorUtil.CHECK_ADD_TO_SCHOOL);
            }
        }
        if (checkNumber == courseIds.length) {
            for (Integer courseId : courseIds) {
                Integer col = AloneCourseToPageDao.insertCourseToPage(courseId, coursePageId, schoolId);
                if (col > 0) {
                    i++;
                }
            }
        } else {
            throw new Exception(ErrorUtil.CHECK_ADD_TO_SCHOOL);
        }
        if (i == courseIds.length) {
            return i;
        } else {
            throw new Exception(ErrorUtil.CHECK_ADD_TO_NUMBER);
        }
    }

    
    /**
     * 根据课程包id和学校查询对应课程
     *
     * @param AloneCourseToPage
     * @return
     */
    @Override
    public LinkedList<Map<String, Object>> getCourseToPage(AloneCourseToPage AloneCourseToPage) throws Exception {
        String schoolId = String.valueOf(ThreadLocalUtil.getEmployee().getSchoolId());
        Assert.notNull(schoolId, ErrorUtil.CHECK_SCHOOL_ID);
        AloneCourseToPage.setSchoolId(schoolId);
        LinkedList<Map<String, Object>> list = AloneCourseToPageDao.getCourseToPage(AloneCourseToPage);
        if (list != null && list.size() != 0) {
            return list;
        } else {
            throw new Exception(ErrorUtil.CHECK_ADD_RESULT);
        }
    }

    
    /**
     * 修改某个课程包对应的课程
     *
     * @param AloneCourseToPage
     * @return
     */
    @Override
    public Integer updateCourseToPage(AloneCourseToPage AloneCourseToPage) throws Exception {
        String schoolId = String.valueOf(ThreadLocalUtil.getEmployee().getSchoolId());
        Assert.notNull(schoolId, ErrorUtil.CHECK_SCHOOL_ID);
        Assert.notNull(AloneCourseToPage.getCourseId(), ErrorUtil.UPDATE_COUSE);
        Assert.notNull(AloneCourseToPage.getCoursePageId(), ErrorUtil.UPDATE_PAGE);
        Integer check = AloneCourseToPageDao.checkCourseToPage(AloneCourseToPage.getCourseId(), AloneCourseToPage.getCoursePageId(), schoolId);
        if (check == 0) {
            return AloneCourseToPageDao.updateCourseToPage(AloneCourseToPage);
        } else {
            throw new Exception(ErrorUtil.CHECK_ADD_TO_SCHOOL);
        }
    }

    /**
     * 删除课程包里的课程
     *
     * @param csToPgId
     * @return
     */
    @Override
    public Integer deleteCourseToPage(Integer csToPgId,Integer coursePageId) throws Exception {
        Integer col = AloneCourseToPageDao.deleteCourseToPage(csToPgId,coursePageId);
        if (col > 0) {
            return col;
        } else {
            throw new Exception(ErrorUtil.DELETE_FAIURE);
        }
    }
    /**
     * 获取未添加到课程包的课程
     * @return
     */
    @Override
    public LinkedList<Map<String, Object>> getOtherCourseToPage(Integer coursePageId) throws Exception {
        Assert.notNull(coursePageId,ErrorUtil.RESULT_VALUE);
        LinkedList<Map<String, Object>> otherCourseToPage = AloneCourseToPageDao.getOtherCourseToPage(coursePageId);
        if (otherCourseToPage != null && otherCourseToPage.size() != 0) {
            return otherCourseToPage;
        } else {
            throw new Exception(ErrorUtil.RESULT_VALUE);
        }
    }
}
